package logic

import (
	"context"
	"errors"
	"server/models"
	"server/proto"
	"strconv"
)

func (s *ServerRpc) AddFruit(ctx context.Context, in *proto.AddFruitReq) (*proto.AddFruitRes, error) {
	var rr proto.AddFruitRes
	//todo: mysql添加
	fruit, err := models.CreateFruit(models.Fruit10{
		Name:      in.Name,
		Title:     in.Title,
		Content:   in.Content,
		Type:      in.Type,
		Inventory: int(in.Inventory),
	})
	if err != nil {
		return &rr, errors.New("mysql添加失败")
	}
	//todo: es添加
	err = models.EsInsert(in.Index, fruit, strconv.Itoa(int(fruit.ID)))
	if err != nil {
		return &rr, errors.New("es添加失败")
	}
	return &rr, nil
}
